package zelix.hack.hacks;

import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.value.BooleanValue;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemSword;
import net.minecraft.network.play.client.CPacketPlayerTryUseItem;
import net.minecraft.network.play.client.CPacketUseEntity;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import zelix.managers.HackManager;
import zelix.utils.Utils;
import zelix.utils.Wrapper;

public class AutoShield extends Hack {
	BooleanValue INTERACT;

	public AutoShield() {
		super("AutoShield", HackCategory.COMBAT);

		INTERACT = new BooleanValue("INTERACT", false);
		this.addValue(INTERACT);
	}

	@Override
	public String getDescription() {
		return "Manages your shield automatically.";
	}

	@Override
	public void onClientTick(ClientTickEvent event) {
		if(!Utils.screenCheck()) { this.blockByShield(false); }
		super.onClientTick(event);
	}

	@Override
	public void onDisable() {
		this.blockByShield(false);
		super.onDisable();
	}

	public void blockByShield(boolean state) {
		if (!(Wrapper.INSTANCE.player().getHeldItemMainhand().getItem() instanceof ItemSword))
			return;

		if (INTERACT.getValue()) {
			if (KillAura.getTarget() != null) {
				Wrapper.INSTANCE.sendPacket(new CPacketUseEntity(KillAura.getTarget(), EnumHand.MAIN_HAND,
						new Vec3d((double) randomNumber(-50, 50) / 100.0D, (double) randomNumber(0, 200) / 100.0D,
								(double) randomNumber(-50, 50) / 100.0D)));
				Wrapper.INSTANCE.sendPacket(new CPacketUseEntity(KillAura.getTarget(), EnumHand.MAIN_HAND));

			}
		}
		if (KillAura.getTarget() != null) {
			Wrapper.INSTANCE.sendPacket(new CPacketPlayerTryUseItem(EnumHand.MAIN_HAND));
			Wrapper.INSTANCE.player().setActiveHand(EnumHand.OFF_HAND);;
			//RobotUtils.setMouse(1, true);
		//	Wrapper.INSTANCE.sendPacket(new CPacketPlayerTryUseItem(EnumHand.OFF_HAND));
			//Wrapper.INSTANCE.sendPacket(new CPacketPlayerTryUseItemOnBlock(new BlockPos(-1, -1, -1), EnumFacing.DOWN, EnumHand.MAIN_HAND, 0, 0, 0));
			//ChatUtils.message("ok");
			//KeyBinding.setKeyBindState(Wrapper.INSTANCE.mc().gameSettings.keyBindUseItem.getKeyCode(), true);
			
			//Wrapper.INSTANCE.controller().onStoppedUsingItem(Wrapper.INSTANCE.player());
		} else {
			KeyBinding.setKeyBindState(Wrapper.INSTANCE.mc().gameSettings.keyBindUseItem.getKeyCode(), false);
			Wrapper.INSTANCE.controller().onStoppedUsingItem(Wrapper.INSTANCE.player());
		}
		KeyBinding.setKeyBindState(Wrapper.INSTANCE.mc().gameSettings.keyBindUseItem.getKeyCode(), false);
		// Wrapper.INSTANCE.sendPacket(new CPacketWPlayerTryUseItemOnBlock(new
		// BlockPos(-1, -1, -1),EnumFacing.DOWN , EnumHand.MAIN_HAND, 0f, 0f, 0f));
	}

	public static void block(boolean state) {
		AutoShield hack = (AutoShield) HackManager.getHack("AutoShield");
		if (hack.isToggled())
			hack.blockByShield(state);
	}

	public static int randomNumber(int max, int min) {
		return Math.round((float) min + (float) Math.random() * (float) (max - min));
	}
}
